<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" lay-filter="myForm"  action="">
    <div class="layui-form-item">
        <label class="layui-form-label required">ID</label>
        <div class="layui-input-block">
            <input type="number" name="id" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            <tip>填写管理账号的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" placeholder="此字段可以为空,填入密码将会覆盖原密码" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="number" name="phone" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">邮箱</label>
        <div class="layui-input-block">
            <input type="email" name="email" lay-verify="required" lay-reqtext="邮箱不能为空" placeholder="请输入邮箱" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <input type="hidden" value="http://r22u4wpeb.hd-bkt.clouddn.com/user_default_image.png" name="icon" id="image">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="imageUploadButton">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="imageImg" style="width: 100px;height: 100px" src="http://r22u4wpeb.hd-bkt.clouddn.com/user_default_image.png">
                    <p id="demoText"></p>
                </div>
                <div style="width: 95px;">
                    <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="imageProgressBar">
                        <div class="layui-progress-bar" lay-percent=""></div>
                    </div>
                </div>
            </div>

            <a name="list-progress"> </a>

            <div style="margin-top: 10px;"></div>

        </div>

    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/md5.js" charset="utf-8"></script>
<script>

    //说明:获取url参数
    //参数obj:访问路径
    //返回值:参数
    var id = getQueryString("id");

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r !== null)
            return unescape(r[2]);
        return null;
    }





    layui.use(['upload','element','form'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            element = layui.element,
            $ = layui.$;

        if (id == null || id == undefined || id == "") {
            layer.alert("id获取失败,页面正常使用,请关闭重试");
        }

        //获取用户信息
        $.post("http://localhost:8080/api/adminUser/getUser",{
            id:id
        },(data)=>{
            if (data.code === 0) {

                //修改表单的值
                form.val("myForm",{
                    "id":data.data.id,
                    "name":data.data.name,
                    "phone":data.data.phone,
                    "email":data.data.email
                });
                //修改图片的默认值
                $("#imageImg").attr("src",data.data.icon);
                $("#image").val(data.data.icon);    //修改隐藏字段的值
            }
        });



        //需要先获取token数据
        var token = "";

        let httpRequest = new XMLHttpRequest();
        httpRequest.open("post","http://localhost:8080/api/common/getUploadToken",false);
        httpRequest.send();
        //结果校验
        var responseText = httpRequest.responseText;
        if (responseText == ""|| (token = JSON.parse(responseText).data ) == undefined || token == "" || token == null){
            layer.alert("token获取失败,可能导致上传失败");
        }


        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#imageUploadButton'
            ,url: 'http://up.qiniu.com' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            ,data:{token:token} //带着上传参数
            ,accept:"images"
            ,acceptMime: "image/*"
            ,size:1024*5        //设置最大大小5m
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#imageImg').attr('src', result); //图片链接（base64）
                });

                element.progress('imageProgressBar', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                let key = res.key;
                $("#image").val(key)    //将数据添加到隐藏的字段里

                $('#demoText').html(''); //置空上传失败的状态
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            //进度条
            ,progress: function(n, elem, e){
                element.progress('imageProgressBar', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });



        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // 对密码进行加密
            if (data.field.password != "") {
                data.field.password = hex_md5(data.field.password);
            }
            //异步请求添加数据
            $.ajax({
                url: "http://localhost:8080/api/adminUser/modifyUser",
                method:"post",
                data:data.field,
                success:function (result) {
                    if (result.code === 0) {
                        //添加成功
                        layer.msg(result.msg,{time: 1000},function () {
                            //关闭窗口
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        })
                    } else {
                        layer.msg("修改失败");
                    }
                },
                error:function (result) {
                    layer.msg("修改失败");
                }
            })

            return false;
        });

    });
</script>
</body>
</html>